#include <stdio.h>
#include <string.h>
#include "font.h"

/* file gen_font.c: ISO-8859 C program text, with CRLF line terminators */
/*
    V0.1
    V1.0 增加汉字和字符的查重功能，重复的汉字或字符将以'//!'注释进行提示 
    V1.1 增加汉字 事件
    V1.2 增加汉字 原因
 */
void sort(short *buffer, int count) 
{
    int i, j, n;
    for (i = 0; i < count - 1; i++) {
        for (j = 0; j < count - i - 1; j++) {
            if (buffer[j] > buffer[j + 1]) {
                n = buffer[j + 1];
                buffer[j + 1] = buffer[j];
                buffer[j] = n;
            }
        }
    }
}

unsigned char font_8x16_def[16];  //存放8x16默认值，字符
const unsigned char font_8x16_buffer[] = " 0123456789ABCDEFINOPTZ>-.,#:?/";

unsigned char font_16x16_def[32]; //存放16x16默认值，汉字
                                       //                    1                   2
                                       // "1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
const unsigned char font_16x16_buffer[] = "：创为新能源科技专注储安全是否有无设备高"\
                                          "册正常报警级别故障启动延放气喷洒打开关闭"\
                                          "停止灭分防护区时间火器系统信息总数编号低"\
                                          "类型输入出干接点版本名称逻辑烟温感当前模"\
                                          "式自手用序日期初始状态效完毕记录远程氧度"\
                                          "快速升缓慢未定义雾阈值事件原因";
unsigned short font_n_buffer[256];

int main() 
{
    int i, j, k;
    int n;
    int find;
    const unsigned char *ptr;    
    const unsigned char *p;
    unsigned short font_n;
    char str[3] = { '\0', '\0', '\0' };  
	char utf8_str[4];
	unsigned int utf8_code;
	  
    if (strlen(font_16x16_buffer) % 2 != 0) {
        return 1;    
    }
       
    printf("#include \"font.h\"\n\n"); 
    printf("const struct font_16x16_t font_16x16[] = {\n");
    
    p = font_16x16_buffer;
    n = 0;
    bzero(font_n_buffer, sizeof(font_n_buffer));
    memset(font_16x16_def, 0xff, sizeof(font_16x16_def));
    while (*p != '\0') { //将汉字转换为UINT16类型
        font_n_buffer[n] = (short)(*p++) << 8;
        font_n_buffer[n] += (short)(*p++);
        n++;   
    }  
    sort(font_n_buffer, n); //排序
	for (i = 1; i < n; i++) { //汉字查重
		if (font_n_buffer[i - 1] == font_n_buffer[i]) {
			printf("//!%04X\n", font_n_buffer[i]); //输出重复汉字
		}
	}
    
    for (i = 0; i < n; i++) {
        find = 0;
        ptr = font_16x16_def; //先设置为默认值，如果查询不到就使用该默认值，默认值全是0xFF
        for (j = 0; j < font_16x16_count; j++) { //查找是否已有该汉字对应的点阵数据
            if (font_n_buffer[i] == font_16x16[j].font_n) { //匹配
                ptr = font_16x16[j].font_c; 
                find = 1;
                break;
            }
        }
        
        str[0] = (char)(font_n_buffer[i] >> 8);
        str[1] = (char)(font_n_buffer[i] & 0xff);

        for (j = 0; j < 32; j++) { 
            if (j == 0) {
                printf("    {0x%04X,{0x%02X,", font_n_buffer[i], ptr[j]);
            } else if (j == 16) {
                printf("\n        0x%02X,", ptr[j]);
            } else if (j == 31) {
                if (find == 1) {
                    printf("0x%02X}}, //%s\n", ptr[j], str);
                } else {
                    printf("0x%02X}}, //%s!!!!!!!!\n", ptr[j], str);                    
                }
            } else {
                printf("0x%02X,", ptr[j]);
            }
        }
    }
    printf("};\n");
    printf("const int font_16x16_count = sizeof(font_16x16) / sizeof(font_16x16[0]);\n\n");
 
    printf("const struct font_8x16_t font_8x16[] = {\n");
    p =  font_8x16_buffer;
    n = 0;
    bzero(font_n_buffer, sizeof(font_n_buffer));
    memset(font_8x16_def, 0xff, sizeof(font_8x16_def)); 
    while (*p != '\0') {
        font_n_buffer[n] = (int)(*p++);
        n++;
    }  
    sort(font_n_buffer, n); //排序
	for (i = 1; i < n; i++) { //字符查找
		if (font_n_buffer[i - 1] == font_n_buffer[i]) {
			printf("//!%02X\n", font_n_buffer[i]); //输出重复字符
		}
	}
    for (i = 0; i < n; i++) {
        find = 0;
        ptr = font_8x16_def; //先设置为默认值，如果查询不到就使用该默认值，默认值全是0xFF
        for (j = 0; j < font_8x16_count; j++) { //查找是否已有该字符对应的点阵数据
            if ((unsigned char)font_n_buffer[i] == font_8x16[j].font_n) { //匹配
                ptr = font_8x16[j].font_c; 
                find = 1;
                break;
            }
        }
        
        str[0] = (char)(font_n_buffer[i]);

        for (j = 0; j < 16; j++) {
            if (j == 0) {
                printf("    {0x%02X,{0x%02X,", font_n_buffer[i], ptr[j]);
            } else if (j == 15) {
                if (find == 1) {                    
                    printf("0x%02X}}, //%c\n", ptr[j], str[0]);
                } else {
                    printf("0x%02X}}, //%c!!!!!!\n", ptr[j], str[0]);                   
                }
            } else {
                printf("0x%02X,", ptr[j]);
            }
        }
    }
    printf("};\n");
    printf("const int font_8x16_count = sizeof(font_8x16) / sizeof(font_8x16[0]);\n\n");
    
    printf("const unsigned char * ascii_index(unsigned char c)\n"); 
    printf("{\n");
    printf("    int i;\n");
    printf("    for (i = 0; i < font_8x16_count; i++) {\n");
    printf("        if (font_8x16[i].font_n == c) {\n");
    printf("            return font_8x16[i].font_c;\n");
    printf("        }\n");
    printf("    }\n");
    printf("    return font_8x16[0].font_c;\n");
    printf("}\n");
    printf("\n");
    printf("const unsigned char * font_index(unsigned short font_n)\n"); 
    printf("{\n");
    printf("    int i;\n");
    printf("    for (i = 0; i < font_16x16_count; i++) {\n");
    printf("        if (font_16x16[i].font_n == font_n) {\n");
    printf("            return font_16x16[i].font_c;\n");
    printf("        }\n");
    printf("    }\n");
    printf("    return font_16x16[0].font_c;\n");  
    printf("}\n");   
           
    return 0;    
}
